package com.java.Servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

// http://localhost:8090/java-web/http-servlet-request?user=syx&age=19
@WebServlet("/http-servlet-request")
public class HttpServletRequestAPI extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // getRequestURI：获取统一资源标识符
        System.out.println(req.getRequestURI()); // /java-web/http-servlet-request

        // getRequestURL：获取统一资源定位符
        System.out.println(req.getRequestURL()); // http://localhost:8090/java-web/http-servlet-request

        // getContextPath：获取上下文路径
        System.out.println(req.getContextPath()); // /java-web

        // getPathInfo：获取路径信息
        System.out.println(req.getPathInfo());

        // getQueryString：获取地址栏参数
        System.out.println(req.getQueryString()); // user=syx&age=19

        // getParameterXxx：获取请求参数
        System.out.println(req.getParameterMap()); // org.apache.catalina.util.ParameterMap@30f21d27
        System.out.println(req.getParameterNames()); // java.util.Collections$3@2eb67d2b
        System.out.println(req.getParameter("user")); // syx
        System.out.println(Arrays.toString(req.getParameterValues("user"))); // [syx]

        // getRequestDispatcher：请求转发
        RequestDispatcher dispatcher = req.getRequestDispatcher("/dispatcher");
        dispatcher.forward(req, resp);
    }
}
